package com.littlebee.storysuper.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.littlebee.storysuper.R;
import com.zss.library.activity.BaseActivity;
import com.zss.library.adapter.BrowserPageAdapter;
import com.zss.library.fragment.BaseFragment;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.viewpager.BrowserPager;

import java.net.URLDecoder;
import java.util.List;

/**
 * Created by zm on 2017/3/29.
 * 图片浏览页面
 */

public class BrowserDetailActivity extends BaseActivity {
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";
    private TextView hint;
    private BrowserPager viewPager;
    private BrowserPageAdapter adapter;
    /**
     * 预浏览图片的开始下标
     */
    private int pos;
    /**
     * 图片地址列表
     */
    private List<String> datas;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_designer_browser;
    }

    @Override
    public void initView() {
        super.initView();
        hint = (TextView) findViewById(R.id.hint);
        viewPager = (BrowserPager) findViewById(R.id.viewPager);
    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgColor(R.color.trans_a);
        toolbar.setTitleColor(R.color.white);
        toolbar.setTitle("项目展示");
        toolbar.setVisibility(View.GONE);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        //Bundle args = getArguments();
        pos = getIntent().getIntExtra(EXTRA_IMAGE_INDEX,0);
        datas = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
        for (int i=0;i<datas.size();i++){
            if (datas.get(i).startsWith("http%3A")) {
                datas.set(i,URLDecoder.decode(datas.get(i)));
            }
        }
        adapter = new BrowserPageAdapter(getActivity(), datas, BrowserPageAdapter.LoadType.URL);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(pos);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pos = position;
                hint.setText(pos + 1 + "/" + datas.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        hint.setText(pos + 1 + "/" + datas.size());
    }

}
